<template>
	<view>
		<cu-custom bgColor="bg-orange" :isBack="true">
			<block slot="backText"></block>
			<block slot="content">单词查询</block>
		</cu-custom>
		<view class="inputbox">
			<text class="flag">英汉互译</text>
			<van-field :value="keyword" placeholder="说点什么吧..." autosize class="input"
				custom-style='border: 1rpx solid #eee;border-radius: 50rpx;background-color: #fff1b9;width:90%;margin-left:15rpx;'
				@change='change' />
			<view class="btn" @click='search'>
				<van-icon name="success" size='50rpx' class="box2" />
			</view>
		</view>
		<view class="content" v-if="keyword">
			<view class="box" v-for="item,index in result" :key="index" @click="clickitem(item,index)">
				<text class="entry">{{item.entry}}</text>
				<text class="explain">{{item.explain}}</text>
			</view>
		</view>
		<view v-else class="history">
			<view class="left">
				<text>搜索记录</text>
			</view>
			<view class="right">
				<view v-for="item,index in historyword" :key="index" class="text" @click="clickhistory(item)">{{item}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapState
	} from 'vuex'
	
	export default {
		data() {
			return {
				keyword: '',
				result: []
			}
		},
		
		onLoad() {
			this.test()
		},
		computed: {
			...mapState('m_word', ['historyword']),
		},
		methods: {
			...mapMutations('m_word', ['updatehistoryword']),
			test() {
				uni.request({
					url: 'http://dict.youdao.com/suggest?q=' + this.keyword + '&num=15&doctype=json',
					method: "GET",
					success: (res) => {
						this.result = []
						this.result = res.data.data.entries
					}
				});
			},
			search() {
				uni.request({
					url: 'http://dict.youdao.com/suggest?q=' + this.keyword + '&num=1&doctype=json',
					method: "GET",
					success: (res) => {
						const item = res.data.data.entries[0]
						const jsonData = JSON.stringify(item);

						uni.navigateTo({
							url: `/subpkg/worddetail/worddetail?item=${encodeURIComponent(jsonData)}`
						})
						const data = [...this.historyword]
						data.push(this.keyword)
						this.updatehistoryword(data)
					}
				});
			},
			change(e) {
				this.keyword = e.detail
				this.test(this.keyword)
			},
			clickitem(item, index) {
				const jsonData = JSON.stringify(item);
				uni.navigateTo({
					url: `/subpkg/worddetail/worddetail?item=${encodeURIComponent(jsonData)}`
				})
				const data = [...this.historyword]
				data.push(item.entry)
				this.updatehistoryword(data)
			},
			clickhistory(item){
				const jsonData = JSON.stringify({entry:item});
				uni.navigateTo({
					url: `/subpkg/worddetail/worddetail?item=${encodeURIComponent(jsonData)}`
				})
			}
		},

	}
</script>

<style lang="less" scoped>
	.inputbox {
		display: flex;
		align-items: center;
		justify-content: space-around;
		margin-top: 35rpx;
		margin-left: 20rpx;

		.flag {
			width: 118rpx;
			font-size: 32rpx;
		}

		.input {
			width: 88%;
		}

		.btn {
			height: 95rpx;
			width: 135rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #FADAD8;
			border-radius: 20rpx;
			margin-right: 20rpx;
		}
	}

	.content {
		width: 90%;
		margin: 20rpx;
	}

	.box {
		font-size: 32rpx;
		height: 50rpx;
		margin-left: 20rpx;
		margin-bottom: 30rpx;
		display: flex;
		overflow: hidden;

		.entry {
			width: 200rpx;
			text-overflow: ellipsis;
			white-space: nowrap;
			overflow: hidden;
			flex-shrink: 0;
		}

		.explain {
			width: 500rpx;
			color: #D9D4CF;
			font-size: 28rpx;
			margin-left: 20rpx;
			text-overflow: ellipsis;
			white-space: nowrap;
			overflow: hidden;
		}
	}

	.history {
		display: flex;
		align-items: center;
		margin: 40rpx;
		font-size: 30rpx;

		.right {
			margin-left: 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-wrap: wrap;
			.text {
				color: #D9D4CF;
				font-size: 32rpx;
				background-color: #f2e9e1;
				border-radius: 13rpx;
				padding: 13rpx 18rpx;
				margin: 10rpx;
			}
		}
	}
</style>